package com.xmb.wechat.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 按微信的方式格式化日期:<p>
 * 今天: HH:mm ，例 8:28
 * <p>
 * 昨天: 昨天 HH:mm， 例 昨天 9:27
 * <p>
 * 近7天 : 星期X HH:mm ，例 星期一 6:25
 * <p>
 * 今年: M月d日 HH:mm 例 3月2日 9:43
 * <p>
 * 往年: yyyy年M月d日 HH:mm 例 2018年6月9日 6:52
 */
public class WechatDateFormat {
    /**
     * 格式化首页的消息列表
     */
    public static String formatOutsideMsgList(Date date) {
        if (date == null)
            return "";
        return formatOutsideMsgList(date.getTime());
    }

    /**
     * 格式化首页的消息列表
     */
    public static String formatOutsideMsgList(Long timestamp) {
        String result = "";
        String[] weekNames = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        String hourTimeFormat = "HH:mm";
        //        String monthTimeFormat = "M月d日 HH:mm";
        //        String yearTimeFormat = "yyyy年M月d日 HH:mm";
        String monthTimeFormat = "M月d日";
        String yearTimeFormat = "yyyy年M月d日";
        try {
            Calendar todayCalendar = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timestamp);

            if (todayCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {//当年
                if (todayCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)) {//当月
                    int temp =
                            todayCalendar.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);
                    switch (temp) {
                        case 0://今天
                            result = getTime(timestamp, hourTimeFormat);
                            break;
                        case 1://昨天
                            //                            result = "昨天 " + getTime(timestamp, hourTimeFormat);
                            result = "昨天 ";
                            break;
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                            result = weekNames[dayOfWeek - 1] + " ";
                            break;
                        default:
                            result = getTime(timestamp, monthTimeFormat);
                            break;
                    }
                } else {
                    result = getTime(timestamp, monthTimeFormat);
                }
            } else {
                result = getTime(timestamp, yearTimeFormat);
            }
            return result;
        } catch (Exception e) {
            Log.e("getTimeString", e.getMessage());
            return "";
        }
    }

    /**
     * 格式化聊天对话里面的日期
     */
    public static String formatInsideChat(Date date, Context context) {
        return formatInsideChat(date.getTime(), context);
    }

    /**
     * 格式化聊天对话里面的日期
     */
    public static String formatInsideChat(Long timestamp, Context context) {
        String result = "";
        String[] weekNames = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        String hourTimeFormat = "HH:mm";
        String monthTimeFormat = "M月d日 HH:mm";
        String monthTimeFormat1 = "M月d日";
        String yearTimeFormat = "yyyy年M月d日 HH:mm";
        String yearTimeFormat1 = "yyyy年M月d日";
        try {
            Calendar todayCalendar = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timestamp);

            if (todayCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {//当年
                if (todayCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH)) {//当月
                    int temp = todayCalendar.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);
                    switch (temp) {
                        case 0://今天
                            if (!DateFormat.is24HourFormat(context)) {//12 小时制
                                String time = getTime(timestamp, "hh:mm");
                                if (time.startsWith("0"))
                                    time = time.substring(1);
                                result = getPreFix(getTime(timestamp, "HH")) + time;
                            } else
                                result = getTime(timestamp, hourTimeFormat);
                            break;
                        case 1://昨天
                            result = "昨天 " + get24FormatTime(DateFormat.is24HourFormat(context), timestamp, hourTimeFormat);
                            break;
                        //                        case 2:
                        //                        case 3:
                        //                        case 4:
                        //                        case 5:
                        //                        case 6:
                        //                            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
                        //                            result = weekNames[dayOfWeek - 1] + " " + getTime(timestamp, hourTimeFormat);
                        //                            break;
                        default:
                            result = getTime(timestamp, monthTimeFormat1) + " " + get24FormatTime(false, timestamp, hourTimeFormat);
                            //                    result = getTime(timestamp, monthTimeFormat);
                            break;
                    }
                } else {
                    result = getTime(timestamp, monthTimeFormat1) + " " + get24FormatTime(false, timestamp, hourTimeFormat);
                    //                    result = getTime(timestamp, monthTimeFormat);
                }
            } else {
                result = getTime(timestamp, yearTimeFormat1) + " " + get24FormatTime(false, timestamp, hourTimeFormat);
                //                result = getTime(timestamp, yearTimeFormat);
            }
            return result;
        } catch (Exception e) {
            Log.e("getTimeString", e.getMessage());
            return "";
        }
    }

    //获取24小时制的时间
    private static String get24FormatTime(boolean is24Hour, Long timestamp, String hourTimeFormat) {
        if (!is24Hour) {
            String time = getTime(timestamp, hourTimeFormat);
            if (time.startsWith("0"))
                time = time.substring(1);
            return getPreFix(getTime(timestamp, "HH")) + time;
        } else
            return getTime(timestamp, hourTimeFormat);
    }

    //区分一天中的上下午...
    private static String getPreFix(String strHour) {
        int hour = Integer.parseInt(strHour);
        if (hour < 7) {
            return "凌晨";
        } else if (hour < 9) {
            return "早上";
        } else if (hour < 12) {
            return "上午";
        } else if (hour < 13) {
            return "中午";
        } else if (hour < 18) {
            return "下午";
        } else {
            return "晚上";
        }
    }

    private static String getTime(long time, String pattern) {
        Date date = new Date(time);
        return dateFormat(date, pattern);
    }

    private static String dateFormat(Date date, String pattern) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 格式红包里显示的时间格式
     *
     * @param date
     * @param simpleDateFormat
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String formatRedPacketTime(Date date, SimpleDateFormat simpleDateFormat) {
        String result = "";
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH) == 0) {//今天
                result = new SimpleDateFormat("HH:mm").format(date);
            } else {
                result = simpleDateFormat.format(date);
            }
            if (result.startsWith("0"))
                result = result.substring(1);
            return result;
        } catch (Exception e) {
            Log.e("getTimeString", e.getMessage());
            return "";
        }
    }

    /**
     * 格式化时间 (当年内不显示年份)
     *
     * @param msgTime
     * @return
     */
    public static String formatWithOutYear(long msgTime) {
        Calendar todayCalendar = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(msgTime);
        if (todayCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)) {//当年
            return getTime(msgTime, "MM-dd HH:mm");
        } else {
            return getTime(msgTime, "yyyy-MM-dd HH:mm");
        }
    }
}
